//
//  YAYIDateCell.m
//  YAYIDoctor
//
//  Created by hxp on 16/5/18.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIDateCell.h"

#import "YAYIDateModel.h"

@interface YAYIDateItemView()

@property (nonatomic, weak) UIView *circleBgView;
@property (nonatomic, weak) UILabel *dayLbl;
@property (nonatomic, weak) UIView *bottomFlagView;

@property (nonatomic, assign) BOOL isSelected;
@property (nonatomic, assign) BOOL isToday;
@property (nonatomic, assign) BOOL isHaveSechedule;

@end

@implementation YAYIDateItemView

- (void)setIsToday:(BOOL)isToday
{
    _isToday = isToday;
    
    if (isToday) {
        self.circleBgView.hidden = NO;
        self.dayLbl.textColor = [UIColor whiteColor];
    }else{
        self.circleBgView.hidden = YES;
        self.dayLbl.textColor = YAYIColor(112, 108, 108);
    }
}


- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    
    if (isSelected) {
        self.circleBgView.hidden = NO;
        self.dayLbl.textColor = [UIColor whiteColor];
    }else if(!self.isToday){
        self.circleBgView.hidden = YES;
        self.dayLbl.textColor = YAYIColor(112, 108, 108);
    }
}


- (void)setIsHaveSechedule:(BOOL)isHaveSechedule
{
    _isHaveSechedule = isHaveSechedule;
    
    self.bottomFlagView.hidden = !isHaveSechedule;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UIView *circleBgView = [[UIView alloc] init];
        circleBgView.backgroundColor = YAYIColor(104, 174, 240);
        circleBgView.layer.masksToBounds = YES;
        circleBgView.hidden = YES;
        [self addSubview:circleBgView];
        self.circleBgView = circleBgView;
        
        UILabel *dayLbl = [[UILabel alloc] init];
        dayLbl.textColor = YAYIColor(112, 108, 108);
        dayLbl.font = [UIFont systemFontOfSize:16];
        dayLbl.textAlignment = NSTextAlignmentCenter;
        [self addSubview:dayLbl];
        self.dayLbl = dayLbl;
        
        UIView *bottomFlagView = [[UIView alloc] init];
        bottomFlagView.backgroundColor = YAYIColor(104, 174, 240);
        bottomFlagView.layer.masksToBounds = YES;
        bottomFlagView.hidden = YES;
        [self addSubview:bottomFlagView];
        self.bottomFlagView = bottomFlagView;
        
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.dayLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [self.circleBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.dayLbl);
        make.width.height.mas_equalTo(24);
    }];
    self.circleBgView.layer.cornerRadius = 12;
    
    [self.bottomFlagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(self).offset(-8);
        make.height.mas_equalTo(2);
    }];
}

@end



@interface YAYIDateCell()

@property (nonatomic, strong) UILabel *monthLbl;
@property (nonatomic, strong) UIView *hLineView;
@property (nonatomic, strong) UIView *dateView;
@property (nonatomic, assign) NSInteger count;

@property (nonatomic, strong) NSMutableArray *itemViewArray;

@end

@implementation YAYIDateCell


- (void)setModelArray:(NSMutableArray *)modelArray
{
    _modelArray = modelArray;
    
    YAYIDateModel *model = [modelArray firstObject];
    self.monthLbl.text = [NSString stringWithFormat:@"%ld年%ld月", model.year, model.month];
    
    CGFloat itemW = SCREEN_width / 7.0;
    CGFloat itemH = 35;
    
    NSInteger firstDayWeek = model.week;
    
    for (int i = 0; i < modelArray.count; i++) {
        
        YAYIDateModel *model = modelArray[i];
        
        int row = (int)(i + firstDayWeek) / 7;
        int colum = (int)(i + firstDayWeek) % 7;
        
        YAYIDateItemView *itemView = self.itemViewArray[i];
        itemView.dayLbl.text = [NSString stringWithFormat:@"%ld", model.day];
        if (model.isToday) {
            itemView.isToday = YES;
        }
        
        if (model.isSchedule) {
            itemView.isHaveSechedule = YES;
        }
        
        [self.contentView addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(itemW * colum);
            make.top.mas_equalTo(itemH * row + 40);
            make.width.mas_equalTo(itemW);
            make.height.mas_equalTo(itemH);
        }];
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelTapGesture:)];
//        [itemView addGestureRecognizer:tap];
    }
}


+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"YAYIDateCell";
    
    YAYIDateCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[YAYIDateCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    return cell;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.itemViewArray = [NSMutableArray array];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubView];
    }
    return self;
}



-(void)setupSubView
{
    UILabel *monthLbl = [[UILabel alloc] init];
    monthLbl.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:monthLbl];
    self.monthLbl = monthLbl;
    
    // line
    UIView *hLineView = [[UIView alloc] init];
    hLineView.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:hLineView];
    self.hLineView = hLineView;
    
    for (int i = 0; i < 31; i++) {
        
        YAYIDateItemView *itemView = [[YAYIDateItemView alloc] init];
        itemView.tag = i + 100;
        [self.contentView addSubview:itemView];
        [self.itemViewArray addObject:itemView];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelTapGesture:)];
        [itemView addGestureRecognizer:tap];
    }
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.monthLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(10);
    }];
    
    [self.hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.bottom.mas_offset(0);
        make.width.mas_equalTo(SCREEN_width);
        make.height.mas_equalTo(0.5);
    }];
}


- (void)selectItemViewAt:(NSInteger)index isSelected:(BOOL)isSelected
{
    YAYIDateItemView *itemView = [self.contentView viewWithTag:index + 100];
    itemView.isSelected = isSelected;
}


- (void)handelTapGesture:(UITapGestureRecognizer *)recongnizer
{
    YAYIDateItemView *itemView = (YAYIDateItemView *)recongnizer.view;
    
    // 调用代理
    if ([self.delegate respondsToSelector:@selector(dateCellItemViewDidClickedWithIndex:indexPath:)]) {
        [self.delegate dateCellItemViewDidClickedWithIndex:itemView.tag - 100 indexPath:self.indexPath];
    }
}


@end
